<?php

namespace Encore\ImageExt\Http\Controllers;

use Encore\ImageExt\Utils\ResponseMedia;
use Encore\ImageExt\Services\MediaService;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Response;
use Symfony\Component\HttpFoundation\Response as FoundationResponse;
use Validator;

class MediaSelectorController extends Controller
{

    protected $mediaService;

    public function __construct(MediaService $mediaService)
    {
        $this->mediaService = $mediaService;
    }

    public function getMediaList(Request $request)
    {

        $validator = Validator::make($request->all(), [
            'keyword' => 'nullable',
            'order' => 'nullable',
            'orderName' => 'nullable',
            'page' => 'required|numeric',
            'pageSize' => 'required|numeric',
            'type' => 'nullable',
        ]);

        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }

        $data = $this->mediaService->getMediaList(
            $request->get('user_id', 0),
            $request->get('keyword'),
            $request->get('order', 'desc'),
            $request->get('orderName', 'id'),
            $request->get('pageSize', '25'),
            $request->get('type')
        );

        return $data;
    }

    public function upload(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'file' => 'required',
            'type' => 'required',           // service or user or other
            'move' => 'nullable'
        ]);

        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }

        $media_obj = $this->mediaService->upload(
            $request->get('uid', 0),
            $request->file('file'),
            json_decode($request->get('move')),
            $request->get('tag', 'admin')
        );

        $data = ResponseMedia::make($media_obj);

        return $this->success($data);
    }

    /**
     * @var int
     */
    protected $statusCode = FoundationResponse::HTTP_OK;

    /**
     * @return mixed
     */
    public function getStatusCode()
    {
        return $this->statusCode;
    }

    /**
     * @param $statusCode
     * @return $this
     */
    public function setStatusCode($statusCode)
    {
        $this->statusCode = $statusCode;
        return $this;
    }

    /**
     * @param $data
     * @param array $header
     * @return mixed
     */
    public function respond($data, $header = [])
    {

        return Response::json($data, $this->getStatusCode(),$header);
    }

    /**
     * @param $status
     * @param array $data
     * @param null $code
     * @return mixed
     */
    public function status($status, array $data, $code = null)
    {

        if ($code) {
            $this->setStatusCode($code);
        }

        $status = [
            'status' => $status,
            'code' => $this->statusCode
        ];

        $data = array_merge($status, $data);
        return $this->respond($data);

    }

    /**
     * @param $message
     * @param int $code
     * @param string $status
     * @return mixed
     */
    public function failed($message, $code = FoundationResponse::HTTP_BAD_REQUEST, $status = 'error')
    {

        return $this->setStatusCode($code)->message($message, $status);
    }

    /**
     * @param $message
     * @param string $status
     * @return mixed
     */
    public function message($message, $status = "success")
    {

        return $this->status($status, [
            'message' => $message
        ]);
    }

    /**
     * @param string $message
     * @return mixed
     */
    public function internalError($message = "Internal Error!")
    {

        return $this->failed($message, FoundationResponse::HTTP_INTERNAL_SERVER_ERROR);
    }

    /**
     * @param string $message
     * @return mixed
     */
    public function created($message = "created")
    {
        return $this->setStatusCode(FoundationResponse::HTTP_CREATED)
            ->message($message);

    }

    /**
     * @param $data
     * @param string $status
     * @return mixed
     */
    public function success($data, $status = "success")
    {

        return $this->status($status, compact('data'));
    }

    /**
     * @param string $message
     * @return mixed
     */
    public function notFond($message = 'Not Fond!')
    {
        return $this->failed($message, Foundationresponse::HTTP_NOT_FOUND);
    }
}
